
import Mock from "mockjs";
let List: any[] = []
const count = 55

for (let i = 0; i < count; i++) {
    List.push(Mock.mock({
        'id': '@id',
        'name': '@cname',
        "date": "@date()",//随机生成日期
        'Address': '@county(true)', // 生成省 市 县组成的地址
        'Email': '@email',//生成邮箱
        "avatar": "@image('200x200','red','#fff','avatar')",//生成图片
        "desc": "@cparagraph()",//描述中文
    }))
}
function getDataList(ctx: any) {
    const params = JSON.parse(ctx.body);
    const { page = 1, size = 10, name, id } = params;
    let mockList: any[] = []
    mockList = List.filter(user => {
        if (name && user.name.indexOf(name) === -1) return false
        return true
    })
    mockList = List.filter(user => {
        if (id && user.id.indexOf(id) === -1) return false
        return true
    })
    const pageList = mockList.slice((page - 1) * size, size * page)
    return {
        code: 0,
        data: pageList,
        total: List.length
    }
}

function deleteData(ctx: any) {
    const { id } = JSON.parse(ctx.body)
    const index = List.findIndex(item => item.id === id)
    List.splice(index, 1)
    const pageList = List.slice(0, 10)
    return {
        code: 0,
        data: pageList,
        message: "删除成功",
        total: List.length
    }
}

function updateData(ctx: any) {
    const { id, name, Email, desc } = JSON.parse(ctx.body)
    List.forEach(item => {
        if (item.id === id) {
            item.id = id
            item.name = name || item.name
            item.Email = Email || item.Email
            item.desc = desc || item.desc
        }
    })
    const pageList = List.slice(0, 10)
    return {
        code: 0,
        data: pageList,
        total: List.length
    }
}

export default {
    getDataList,
    deleteData,
    updateData
}